import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:lz_app_core/constants/app_manager.dart';
import 'package:lz_router/kd_router.dart';
import 'package:lz_ui/kd_ui.dart';
import 'package:get/get.dart';

/// 用户协议
class KDUserProtocalWidget extends StatelessWidget {
  final bool isWrap;
  final Function(bool) checkCall;
  final bool showcheck;
  final double? font;
  final String? title;
  final Color? titleColor;
  final bool value;
  KDUserProtocalWidget(
      {super.key,
      this.value = false,
      this.isWrap = false,
      this.font,
      this.title,
      this.titleColor,
      required this.checkCall,
      this.showcheck = true}) {
    agreeProtol = value.obs;
  }

  /// 同意隐私协议的按钮
  var agreeProtol = false.obs;
  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Visibility(
          visible: showcheck,
          child: GestureDetector(
            child: Container(
              width: KD.size(40),
              height: KD.size(40),
              color: Colors.white,
              alignment: Alignment.centerLeft,
              child: Obx(() => Icon(
                    agreeProtol.value ? KDIcon.check : KDIcon.uncheck,
                    color: agreeProtol.value ? KDColor.main : KDColor.place,
                    size: KD.size(32),
                  )),
            ),
            onTap: () {
              agreeProtol.toggle();
              checkCall(agreeProtol.value);
            },
          ),
        ),
        SizedBox(
          width: KD.size(showcheck ? 4 : 0),
        ),
        Expanded(
          child: Text.rich(TextSpan(
              text: title ?? "我已经阅读并同意",
              recognizer: TapGestureRecognizer()
                ..onTap = () {
                  agreeProtol.toggle();
                  checkCall(agreeProtol.value);
                },
              style: TextStyle(
                  fontSize: font ?? KD.font(24),
                  color: titleColor ?? KDColor.place),
              children: [
                TextSpan(
                    text: "《用户服务协议》",
                    recognizer: TapGestureRecognizer()..onTap = _userProtocal,
                    style: TextStyle(color: KDColor.mainBlue)),
                const TextSpan(text: "及"),
                TextSpan(
                    text: "${isWrap ? '\n' : ''}《隐私政策》",
                    recognizer: TapGestureRecognizer()..onTap = _priveProtocal,
                    style: TextStyle(color: KDColor.mainBlue)),
              ])),
        ),
      ],
    );
  }

  /// 用户协议
  _userProtocal() {
    KDWebViewPage page = KDWebViewPage.webPage(
        title: "用户服务协议", url: AppManager.webUrlModel.userlPrototal);
    Get.to(() => page);
  }

  /// 隐私协议
  _priveProtocal() {
    KDWebViewPage page = KDWebViewPage.webPage(
        title: "隐私政策", url: AppManager.webUrlModel.priveProtocal);
    Get.to(() => page);
  }
}
